package cn.dada.dadarecipes.community.thumbsup.service.impl;

import cn.dada.dadarecipes.base.enums.ArticleThumbsUpStatusEnum;
import cn.dada.dadarecipes.base.exception.ServiceException;
import cn.dada.dadarecipes.base.response.StatusCode;
//import cn.dada.dadarecipes.base.util.LoginValidation;
import cn.dada.dadarecipes.community.article.mapper.ArticleMapper;
import cn.dada.dadarecipes.community.article.pojo.entity.Article;
import cn.dada.dadarecipes.community.article.pojo.vo.ArticleVO;
import cn.dada.dadarecipes.community.thumbsup.mapper.ThumbsUpMapper;
import cn.dada.dadarecipes.community.thumbsup.pojo.dto.ThumbsUpSaveParam;
import cn.dada.dadarecipes.community.thumbsup.pojo.entity.ThumbsUp;
import cn.dada.dadarecipes.community.thumbsup.service.ThumbsUpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@Transactional
public class ThumbsUpServiceImpl implements ThumbsUpService {
    @Autowired
    private ThumbsUpMapper thumbsUpMapper;
    @Autowired
    private ArticleMapper articleMapper;


    /**
     * 业务层-文章点赞
     * @param thumbsUpSaveParam
     * @param session
     */
    @Override
    public void saveArticleThumbsUp(ThumbsUpSaveParam thumbsUpSaveParam, HttpSession session) {
        log.debug("业务层-文章点赞: {}", thumbsUpSaveParam);
        /* 用户是否为当前用户登录校验 */
//        LoginValidation.isMyselfLogin(thumbsUpSaveParam.getUserId(), session);

        /* 文章存在校验 和 当前用户是否为文章作者校验 */
//        validationArticle(thumbsUpSaveParam);

        /* 查询点赞记录 */
        ThumbsUp thumbsUp = new ThumbsUp();
        BeanUtils.copyProperties(thumbsUpSaveParam, thumbsUp);
        ThumbsUp info = thumbsUpMapper.selectThumbsUp(thumbsUp);
        //校验传输的点赞id与数据库点赞id是否一致
        if (info != null && !info.getUserId().equals(thumbsUp.getUserId())) {
            log.debug("点赞人于点赞记录不一致");
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }

        /* 新增或取消文章点赞 */
        if (info == null) {
            articleThumbsUp(thumbsUp);//新增
        }else {
            cancelArticleThumbsUp(thumbsUp);//取消
        }
    }
    /**
     * 文章存在校验
     * 当前用户是否为文章作者校验
     * @param thumbsUpSaveParam
     */
    private void validationArticle(ThumbsUpSaveParam thumbsUpSaveParam) {
        /* 文章是否存在 */
        Article article = new Article();
        article.setId(thumbsUpSaveParam.getArticleId());
        List<ArticleVO> articleVOS = articleMapper.selectArticle(article);
        if (articleVOS.isEmpty()){
            throw new ServiceException(StatusCode.ARTICLE_NOT_EXIST);// 文章不存在
        }
        /* 判断当前用户是否为文章作者 */
        if (articleVOS.get(0).getId().equals(thumbsUpSaveParam.getUserId())){
            throw new ServiceException(StatusCode.OPERATION_FAILED);// 文章作者不能给自己点赞
        }
    }
    /**
     * 新增点赞
     * @param thumbsUp
     */
    private void articleThumbsUp(ThumbsUp thumbsUp) {
        Boolean isSuccess;
        /* 判断传输的点赞状态为点赞 */
        if (!ArticleThumbsUpStatusEnum.THUMBS_UP.getCode().equals(thumbsUp.getStatus())) {
            throw new ServiceException(StatusCode.THUMBS_UP_FAILED);// 点赞失败
        }

        /* 新增点赞数据 */
        thumbsUp.setCreateTime(LocalDateTime.now());
        isSuccess = thumbsUpMapper.insertThumbsUp(thumbsUp);
        log.debug("业务层-文章点赞-新增点赞是否成功: {}",isSuccess);
        if (!isSuccess) throw new ServiceException(StatusCode.THUMBS_UP_FAILED);// 点赞失败

        /* 修改文章点赞数[+1] */
        isSuccess = reviseArticleThumbsUpNumber(thumbsUp.getArticleId(),+1);// +1点赞
        log.debug("业务层-文章点赞-文章点赞数是否成功: {}",isSuccess);
        if (!isSuccess) {// 修改失败,数据回滚
            thumbsUpMapper.deleteThumbsUp(thumbsUp.getUserId(),thumbsUp.getArticleId());// 删除点赞记录
            throw new ServiceException(StatusCode.THUMBS_UP_FAILED);// 点赞失败
        }
    }
    /**
     * 取消点赞
     * @param thumbsUp
     */
    private void cancelArticleThumbsUp(ThumbsUp thumbsUp) {
        Boolean isSuccess;
        /* 判断传输的点赞状态为取消点赞 */
        if (!ArticleThumbsUpStatusEnum.CANCEL_THUMBS_UP.getCode().equals(thumbsUp.getStatus())) {
            throw new ServiceException(StatusCode.THUMBS_UP_CANCEL_FAILED);// 取消点赞失败
        }

        /* 修改文章点赞数[-1] */
        isSuccess = reviseArticleThumbsUpNumber(thumbsUp.getArticleId(),-1);// -1点赞
        log.debug("业务层-文章点赞-文章取消点赞数是否成功: {}",isSuccess);
        if (!isSuccess) {// 修改失败,数据回滚
            throw new ServiceException(StatusCode.THUMBS_UP_CANCEL_FAILED);// 取消点赞失败
        }

        /* 删除点赞记录=>取消点赞 */
        thumbsUpMapper.deleteThumbsUp(thumbsUp.getUserId(),thumbsUp.getArticleId());// 删除点赞记录
    }
    /**
     * 修改文章点赞数
     * @param articleId 文章id
     * @param num 点赞+1/取消点赞-1
     * @return 是否操作成功
     */
    private Boolean reviseArticleThumbsUpNumber(Long articleId, int num) {
        return articleMapper.updateArticleLikesTotalNums(articleId,num);
    }
}
